package com.jinlejia.erp.ui.home;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.databinding.ActivityBusinessHelpBinding;
import com.jinlejia.erp.ui.common.ShareDialog;

/**
 * 业务帮助（activity）
 */

public class BusinessHelpActivity extends AppCompatActivity {

    private String cUrl;
    private String url_icon = ApiConfig.HEADER_URL()+"T1BoDVBy_T1RXrhCrK.jpg";
    private ShareDialog shareDialog;

    private ActivityBusinessHelpBinding binding;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_business_help);
        binding.backBtn.setOnClickListener(this::onClick);
        binding.shareBtn.setOnClickListener(this::onClick);
        initView();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        //方式1. 加载一个网页：
        binding.webView.loadUrl("http://mhelp.erp.jinlejia.com/help.html");
//        //方式2：加载apk包中的html页面
//        webView.loadUrl("file:///android_asset/test.html");

        //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        WebSettings webSettings = binding.webView.getSettings();

        //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true);

        //设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小

        //缩放操作
        webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放

        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式

        binding.webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                cUrl = url;
                return true;
            }
            //开始载入页面调用的，我们可以设定一个loading的页面，告诉用户程序在等待网络响应。
            @Override
            public void  onPageStarted(WebView view, String url, Bitmap favicon) {
                //设定加载开始的操作
            }
            //在页面加载结束时调用。我们可以关闭loading 条，切换程序动作。
            @Override
            public void onPageFinished(WebView view, String url) {
                //设定加载结束的操作
            }

        });
        binding.webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if( title.contains("-JinLeJia") && cUrl.contains("wenzhang_") ){
                    binding.tvTitle.setText(title.replace("-JinLeJia",""));
                    //显示分享按钮
                    binding.shareBtn.setVisibility(View.VISIBLE);
                    String smsBody = binding.tvTitle.getText().toString().concat("请查看:").
                            concat(view.getUrl()).concat("。来自【进了家】");
                    shareDialog = new ShareDialog(BusinessHelpActivity.this,
                            binding.tvTitle.getText().toString(),
                            "",smsBody,"","",
                            R.drawable.ic_share_jlj,view.getUrl(),url_icon);
                }else{
                    binding.tvTitle.setText(title);
                    //隐藏分享
                    binding.shareBtn.setVisibility(View.INVISIBLE);
                }
            }

        });
    }


    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back_btn:
                if(binding.webView.canGoBack()){
                    binding.webView.goBack();//返回上一页面
                }else {
                    finish();
                }
                break;
            case R.id.share_btn:
                shareDialog.show();
                break;
            default:
                break;
        }

    }

    //改写物理按键——返回的逻辑
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK)
        {
            if(binding.webView.canGoBack())
            {
                binding.webView.goBack();//返回上一页面
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        if (binding.webView != null) {
            binding.webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            binding.webView.clearHistory();

            ((ViewGroup) binding.webView.getParent()).removeView(binding.webView);
            binding.webView.destroy();
        }
        super.onDestroy();
    }

}
